import Nerv from "nervjs";
import Taro from "@tarojs/taro-h5";
import { View, Text, Block } from '@tarojs/components';
import useBasePage from '../basePage/useBasePage';
import { observer } from "@tarojs/mobx-h5";
import { goToErrorPage, redirectToPage } from '../../utils/index';

class Error extends Taro.Component {
  render() {
    const { params } = Taro.useRouter();
    const { onload, awtRs } = useBasePage();
    const [pageTitle, setPageTitle] = Taro.useState('访问异常');
    const [init, setInit] = Taro.useState(false);
    const [pageInfo, setPageInfo] = Taro.useState({ msg: '' });
    Taro.useEffect(() => {
      onload(async () => {
        console.log('参数：', params);
        switch (params.type) {
          case '400':
            setPageInfo({ msg: '请求参数有误' });
            break;
          case '404':
            setPageInfo({ msg: '请求的资源不存在' });
            break;
          case '405':
            setPageInfo({ msg: '没有权限进入该页面' });
            break;
          case '500':
            setPageInfo({ msg: '系统开⼩差' });
            break;
          case '501':
            setPageInfo({ msg: '合法域名未配置' });
            break;
          case '504':
            setPageInfo({ msg: '网络不给力，请稍后再试' });
            break;
          case '505':
            setPageInfo({ msg: '程序配置异常，加载失败' });
            break;
          default:
            setPageInfo({ msg: '未知错误，状态码：' + params.type });
        }
        if (params.msg) setPageInfo({ msg: params.msg });
        setInit(true);
      }, pageTitle);
    }, []);
    // 返回首页
    const goBackIndex = () => {
      redirectToPage('/pages/home/index');
    };
    return <View className="page">
      {init && <Nerv.Fragment>
          <Text>{pageInfo.msg}</Text>
          <View onClick={goBackIndex}>返回首页</View>
          <View onClick={() => goToErrorPage(404, '122')}>模拟报错</View>
        </Nerv.Fragment>}
    </View>;
  }

  componentDidMount() {
    super.componentDidMount && super.componentDidMount();
  }

  componentDidShow() {
    super.componentDidShow && super.componentDidShow();
  }

  componentDidHide() {
    super.componentDidHide && super.componentDidHide();
  }

}

export default observer(Error);